<template>
  <div class="authen">
    <div></div>
    <span>{{ tip }}</span>
  </div>
</template>

<script>
// 暂时不用
import Vue from "vue";
import { getAuthen } from "@/utils/getAuthen";
import querystring from "querystring";
import { mapMutations, mapActions } from "vuex";
import { getEnterpriseId } from "@/utils/getEnterpriseId";
import settings from "@/settings";

export default Vue.extend({
  head() {
    return {
      title: "认证"
    };
  },
  data() {
    return {
      redirect: "",
      tip: "获取中...",
      openid: "",
      mobilePhone: ""
    };
  },
  async created() {
    this.tip = "获取中...";
    this.openid = this.$route.query.openid;

    if (!!this.openid) {
      this.tip = this.openid;
    } else {
      const e = this.getEnterpriseId();
      this.goto(e);
    }
  },
  methods: {
    getEnterpriseId() {
      // 优先拿网址上的id
      const arrId = window.location.pathname
        .replace(/^\//, "")
        .match(/^\d{1,}/g); // 拿站点
      return arrId[0];
    },
    goto(enterpriseId) {
      $nuxt.$store.dispatch("authen/getAppId", enterpriseId).then(() => {
        sessionStorage.setItem("enterpriseId", enterpriseId);
        $nuxt.$store.commit("authen/setEnterpriseId", enterpriseId);
        window.location.href =
          "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
          $nuxt.$store.getters["authen/appId"] +
          "&redirect_uri=" +
          settings.baseUrl +
          "openId&response_type=code&scope=snsapi_base#wechat_redirect";
      });
    }
  }
});
</script>

<style>
.authen {
  width: 100vw;
  height: 100vh;
  position: relative;
}
.authen div {
  position: absolute;
  top: 2.38rem;
  left: 2.7rem;
  width: 1.82rem;
  height: 2.16rem;
  background: url("")
    no-repeat 0 0;
  background-size: 100% 100%;
}
.authen span {
  position: absolute;
  top: 5.22rem;
  width: 7.5rem;
  text-align: center;
  height: 0.56rem;
  font-size: 0.4rem;
  font-weight: 550;
  color: #000000;
  line-height: 0.56rem;
  font-family: PingFangSC-Regular, PingFang SC;
}
</style>
